/*
 * @Author: qin
 * @Date:   2018-08-28
 * +----------------------------------------------------------------------
 * | admin [ 后台管理系统公共配置信息 ]
 * | 项目相关接口模块
 * +----------------------------------------------------------------------
 */
layui.define(['jquery', 'configure', 'configureCom', 'serializeObject', 'selectFun', 'moduleApi', 'sessionFun', 'table', 'bluebird'], function (exports) {
    var configure = layui.configure;
    var configureCom = layui.configureCom;
    var table = layui.table;
    var selectFun = layui.selectFun;
    var moduleApi = layui.moduleApi;
    var sessionFun = layui.sessionFun;
    var deal = {
        //三方
        busPop: function (status, data) {
            var id = data.id;
            var singleProjectId = data.singleProjId;
            var singleProjName = encodeURIComponent(data.singleProjName);
            var acceptCode = encodeURIComponent(data.acceptCode) || '';

            var url = configure.BUS_DEAL_POP_HTML + '?status=' + status + '&id=' + id + '&singleProjectId=' + singleProjectId + '&acceptCode=' + acceptCode + '&singleProjName=' + singleProjName;
            var title;
            var btn = 1;
            if (status === 1010) {
                title = '拒绝受理';
                btn = '确认拒绝受理';
            } else {
                title = '同意受理';
                btn = '确认同意受理';
            }
            configureCom.klmyPop([url, title, btn, 2]);
        },
        busReload: function (type) {
            var tdWidth = 100;
            var cols = [[ //表头
                {field: 'acceptCode', title: '受理编号'},
                {
                    title: '单位工程名称', templet: function (d) {
                        return configureCom.getMSName(d) + '<span></br>编号：' +
                            d.singleProjCode + '</span>';
                    }
                },
                {
                    title: '建设单位', templet: function (d) {
                        var html = '';
                        html += d.jsdwName;
                        if (d.country) {
                            html += '</br><span>区划：' + selectFun.getAreaByCode(d.country) + '</span>';
                        }
                        return html;
                    }
                }, {
                    title: '勘察设计单位', templet: function (d) {
                        var html = '';
                        $.each(d.kcSjdwMsgDtos, function (index, item) {
                            var typeName = (item.projBdstType === '1010' ? '勘 ' : '设 ');
                            html += typeName + item.orgName + '</br><span>' + configureCom.dateToStrAll(item.confirmDate) + '</span></br>';
                        });
                        return html;
                    }
                },
                {
                    title: '报审时间', width: 170, templet: function (d) {
                        return configureCom.dateToStrAll(d.sendStTime);
                    }
                },
                {title: '操作', width: tdWidth, align: 'center', toolbar: '#barDemo'}
            ]];
            var url = configure.BUS_DEAL_LIST_HTTP;
            var data = {
                singleProjName: '',
                reportType: type //1010代办 ，1020 已同意 ，1030 已拒绝
            };
            var iframeName = '';
            if (type == '1010') {
                iframeName = 'waitDeal';
            } else if (type == '1020') {
                iframeName = 'acceptDeal';
            } else if (type == '1030') {
                iframeName = 'rejectDeal';
            }
            configureCom.tableRender(url, data, cols).then(function (res) {
                if (!configureCom.GetQueryString('addPadding')) {
                    var count = res.count;
                    parent.resetTab(iframeName, count);
                }
            });
        },
        busSaveForm: function () {
            window.saveForm = function (data) {
                var url = configure.BUS_DEAL_SUBMIT_HTTP;
                configureCom.putDataAjax(url, data).then(function (data) {
                    configureCom.loadOff();
                    if (data.code === 1) {
                        top.layer.closeAll();
                        layer.msg('提交成功');
                        configureCom.tableReload(false);
                        try {
                            parent.refreshIframe();
                        } catch (e) {
                        }
                    } else {
                        top.layer.msg(data.msg);
                    }
                });
            };
        },
        busTool: function () {
            table.on('tool(klmyTable)', function (obj) {
                var data = obj.data;
                var btn;
                if (obj.event === 'detail') {//查看详情
                    deal.showDetail(data.singleProjId, btn, data);
                } else if (obj.event === 'agreeAcc') {//同意
                    btn = '';
                    deal.showDetail(data.singleProjId, btn, data);
                } else if (obj.event === 'refuseAcc') {//拒绝
                    // btn = ['拒绝受理'];
                    btn = '';
                    deal.showDetail(data.singleProjId, btn, data);
                } else if (obj.event === 'accept') {//受理
                    btn = ['同意受理', '拒绝受理'];
                    deal.showDetail(data.singleProjId, btn, data);
                }
            });
        },

        //联审
        unitePop: function (type, typeOrStatus, data) {
            var title, url;
            var id = data.id;
            var singleProjName = data.singleProjName;// 单位工程名称
            var acceptMan = data.acceptMan;//受理人姓名
            var acceptCode = data.acceptCode;//受理编号
            var tel = data.applayManTel || '';
            var btn = 1;
            if (type === 'one') {//第一次受理
                url = configure.UNITE_DEAL_POP_HTML + '?reportType=' + typeOrStatus + '&singleProjectId=' + id + '&singleProjName=' + encodeURIComponent(singleProjName);
                if (typeOrStatus === 1030) {
                    title = '分发';
                    btn = '确认分发';
                } else if (typeOrStatus === 1055) {
                    title = '拒绝受理';
                    btn = '确认拒绝受理';
                }
            } else {//最终受理
                url = configure.UNITE_DEAL_POP_HTML + '?status=' + typeOrStatus + '&acceptCode=' + acceptCode + '&singleProjectId=' + id + '&singleProjName=' + encodeURIComponent(singleProjName) + '&acceptMan=' + encodeURIComponent(acceptMan) + '&tel=' + tel;
                if (typeOrStatus === 1010) {
                    title = '拒绝受理';
                    btn = '确认拒绝受理';
                } else if (typeOrStatus === 1020) {
                    title = '同意受理';
                    btn = '确认同意受理';
                }
            }
            configureCom.klmyPop([url, title, btn, 2]);
        },
        uniteReload: function (cols, type,name) {
            var url = configure.UNITE_DEAL_LIST_HTTP;
            var data = {
                singleProjName: '',
                reportType: type //1020待办,1030已分发，1055已拒绝
            };
            var iframeName = '';
            if(type=='1010'){
                iframeName = 'waitDeal';
            } else if (type == '1020') {
                iframeName = 'acceptDeal';
                if (configureCom.GetQueryString('fromDb')) {
                    data.singleProjId = JSON.parse(localStorage.dbData).singleProjId;
                }
            } else if (type == '1030') {
                iframeName = 'rejectDeal';
            }
            configureCom.tableRender(url, data, cols, false, 5).then(function (res) {
                if (!configureCom.GetQueryString('addPadding')) {
                    var count = res.count;
                    try{
                        parent.resetTab(name, count);
                    }catch(err){
                        top.childNumInit();
                    }

                }
            });
        },
        uniteSaveForm: function (type) {
            window.saveForm = function (data) {
                var url;
                if (type === 'one') {
                    url = configure.UNITE_DEAL_ONESUBMIT_HTTP;
                } else {
                    url = configure.UNITE_DEAL_TWOSUBMIT_HTTP;
                }
                configureCom.putDataAjax(url, data).then(function (data) {
                    if (data.code === 1) {
                        top.layer.closeAll();
                        layer.msg('提交成功');
                        configureCom.tableReload();
                        try {
                            parent.refreshIframe();
                        } catch (e) {

                        }
                    } else {
                        top.layer.msg(data.msg);
                    }
                });
            };
        },
        // 查看详情
        showDetail: function (singleProjectId, btn, data) {
            //获取报审详情
            configureCom.getAjax(configure.REPORT_MASTERIA_DETAIL_HTTP.replace('{singleProjectId}', singleProjectId), {singleProjectId: singleProjectId}).then(function (res) {
                var jointTrial = null;//联审对象
                var housingConstruction = null;//住建对象
                $(res.result).each(function (index, item) {
                    if (item.acceptType === configure.AUDIT_ORGAN_TYPE.JOINT_TRIAL.value) {
                        jointTrial = item;
                    }
                    if (item.acceptType === configure.AUDIT_ORGAN_TYPE.HOUSING_CONSTRUCTION.value) {
                        housingConstruction = item;
                    }
                    //审查时间
                    if (item.adviseTime) {
                        item.adviseTime = configureCom.dateToStrAll(item.adviseTime);
                    } else {
                        item.adviseTime = '-';
                    }
                });
                // jointTrial.orgName = housingConstruction.orgName;
                localStorage.bsData = JSON.stringify(res.result);

                var url = configure.ALL_COMMON_DETAIL + '?bsShow=1&projBdstId=&singleProjectId=' + singleProjectId;
                var title = '报审详情';
                // configureCom.klmyPop([url, title]);
                top.layer.open({
                    type: 2,
                    content: url,
                    area: configureCom.Psize1,
                    btnAlign: 'c',
                    btn: btn,
                    success: function (layero, index) {
                        var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        iframeWin.parWin = window; //父页面
                        iframeWin.popIndex = index; //弹窗的index (用于关闭当前弹窗);
                    },
                    yes: function (index, layero) {//同意受理 || 拒绝受理
                        var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        iframeWin.parWin = window;
                        if (btn[0] === '同意受理') {
                            deal.busPop(1020, data);
                        } else if (btn[0] === '拒绝受理') {
                            deal.busPop(1010, data);
                        }
                        top.layer.close(index);
                    }, btn2: function (index, layero) {//拒绝受理
                        var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        iframeWin.parWin = window;
                        deal.busPop(1010, data);
                        top.layer.close(index);
                    }
                });
            }, function (err) {
                return false;
            });
        }
    };
    exports('deal', deal);
});